package com.wenbai.servicelightluxurystickers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wenbai.servicelightluxurystickers.entity.StickUser;
import com.wenbai.servicelightluxurystickers.mapper.StickUserMapper;
import com.wenbai.servicelightluxurystickers.service.StickUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StickUserServiceImpl implements StickUserService {

    @Autowired
    private StickUserMapper stickUserMapper;

    private static final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public StickUser login(String account, String password) {
        QueryWrapper<StickUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        StickUser user = stickUserMapper.selectOne(queryWrapper);
//        对比  明文 ：加密
        if (user != null && passwordEncoder.matches(password, user.getPasswordHash())) {
            return user;
        }
        return null;
    }

    @Override
    public boolean register(StickUser user) {
        // 先查账号是否存在
        QueryWrapper<StickUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", user.getAccount());
        StickUser existingUser = stickUserMapper.selectOne(queryWrapper);
        if (existingUser != null) {
            return false;
        }
        // 加密密码
        user.setPasswordHash(passwordEncoder.encode(user.getPasswordHash()));
        int insert = stickUserMapper.insert(user);
        return insert > 0;
    }

    @Override
    public boolean update(StickUser user) {
        QueryWrapper<StickUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", user.getAccount());
        StickUser existingUser = stickUserMapper.selectOne(queryWrapper);
        if (existingUser == null) {
            return false;
        }
        int update = stickUserMapper.updateById(user);
        return update > 0;
    }

    @Override
    public boolean updatePassword(String account, String oldPassword, String newPassword) {
        QueryWrapper<StickUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        StickUser user = stickUserMapper.selectOne(queryWrapper);
        if (user == null) {
            return false;
        }
        if (!passwordEncoder.matches(oldPassword, user.getPasswordHash())) {
            return false;
        }
        user.setPasswordHash(passwordEncoder.encode(newPassword));
        int update = stickUserMapper.updateById(user);
        return update > 0;
    }

    @Override
    public List<StickUser> selectUser() {
        QueryWrapper<StickUser> queryWrapper = new QueryWrapper<>();
        List<StickUser> user = stickUserMapper.selectList(queryWrapper);
        return user;
    }

    @Override
    public boolean insertUser(StickUser user) {
        LambdaQueryWrapper<StickUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StickUser::getAccount, user.getAccount());
        StickUser existingUser = stickUserMapper.selectOne(queryWrapper);
        if (existingUser != null) {
            return false;
        }
        user.setPasswordHash(passwordEncoder.encode(user.getPasswordHash()));
        user.setCreateTime(LocalDateTime.now());
        int insert = stickUserMapper.insert(user);
        return insert > 0;
    }

    @Override
    public boolean deleteUser(String userId) {
        int delete = stickUserMapper.deleteById(userId);
        return delete > 0;
    }
}